package com.ywk.miaohsa.controller;

import com.sun.org.apache.bcel.internal.classfile.Code;
import com.ywk.miaohsa.domain.MiaoshaUser;
import com.ywk.miaohsa.domain.OrderInfo;
import com.ywk.miaohsa.result.CodeMsg;
import com.ywk.miaohsa.result.Result;
import com.ywk.miaohsa.service.GoodsService;
import com.ywk.miaohsa.service.OrderService;
import com.ywk.miaohsa.vo.GoodsVo;
import com.ywk.miaohsa.vo.OrderDetailVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;

@Controller
@RequestMapping("/order")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    GoodsService goodsService;

    @RequestMapping(value = "/orderDetail",method = RequestMethod.GET)
    @ResponseBody
    public Result<OrderDetailVo> orderDetail(MiaoshaUser user){
        if (user == null) {
            Result.error(CodeMsg.SESSION_ERROR);
        }
        OrderInfo orderInfo = orderService.getOrderInfo(user.getId());
        if (orderInfo == null) {
            Result.error(CodeMsg.MOBILE_NOT_EXIST);
        }
        GoodsVo goodsVo = goodsService.getGoodsVoByGoodsId(orderInfo.getGoodsId());
        OrderDetailVo orderDetailVo=new OrderDetailVo();
        orderDetailVo.setOrder(orderInfo);
        orderDetailVo.setGoods(goodsVo);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        orderDetailVo.setCreateDate(sdf.format(orderInfo.getCreateDate()));
        return Result.success(orderDetailVo);
    }
}
